package com.chenxys.algorithm.leetcode;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 *      <a href="https://leetcode.cn/problems/split-strings-by-separator/">按分隔符拆分字符串</a><br/>
 *      给你一个字符串数组 words 和一个字符 separator ，请你按 separator 拆分 words 中的每个字符串。<br/>
 *      返回一个由拆分后的新字符串组成的字符串数组，不包括空字符串 。<br/>
 *      注意<br/>
 *          separator 用于决定拆分发生的位置，但它不包含在结果字符串中。<br/>
 *          拆分可能形成两个以上的字符串。<br/>
 *          结果字符串必须保持初始相同的先后顺序。<br/>
 *      例: <br/>
 *          输入：words = ["one.two.three","four.five","six"], separator = "."<br/>
 *          输出：["one","two","three","four","five","six"]<br/>
 *          解释：在本示例中，我们进行下述拆分：<br/>
 *              "one.two.three" 拆分为 "one", "two", "three"<br/>
 *              "four.five" 拆分为 "four", "five"<br/>
 *              "six" 拆分为 "six"<br/>
 *          因此，结果数组为 ["one","two","three","four","five","six"] 。<br/>
 * </p>
 * @author Chenxys
 * @version V1.0
 * @Package com.chenxys.algorithm.leetcode
 * @date 2024/1/20 09:21
 * @Copyright ©
 */
@SuppressWarnings("all")
public class LeetCode2788 {

    /**
     * <p>
     *     按分隔符拆分字符串
     * </p>
     */
    public static  List<String> splitWordsBySeparator(List<String> words, char separator) {
        if (Objects.isNull(words) || words.isEmpty()) return null;
        List<String> res = new ArrayList<>();
        if (separator < 65 || (separator > 90 && separator < 97) || separator > 122) {
            String splitKey = "\\" + separator;
            for (String word: words) {
                if (word.contains(String.valueOf(separator))) {
                    if (!Objects.isNull(splitWord(word, splitKey))) res.addAll(splitWord(word, splitKey));
                } else {
                    if (!Objects.isNull(word) && word.length() > 0) res.add(word);
                }
            }
        } else {
            String splitKey = String.valueOf(separator);
            for (String word: words) {
                if (word.contains(String.valueOf(separator))) {
                    if (!Objects.isNull(splitWord(word, splitKey))) res.addAll(splitWord(word, splitKey));
                } else {
                    if (!Objects.isNull(word) && word.length() > 0) res.add(word);
                }
            }
        }
        return res;
    }

    public static List<String> splitWord(String word, String splitKey) {
        List<String> res = new ArrayList<>();
        String[] strings = word.split(splitKey);
        if (strings.length <= 0) return null;
        else {
            for (int i = 0; i < strings.length; i++) {
                if (!Objects.isNull(strings[i]) && strings[i].length() > 0) res.add(strings[i]);
            }
        }
        return res;
    }

    /**
     * <p>
     *     按分隔符拆分字符串
     * </p>
     */
    public static List<String> checkSplitWordsBySeparator(List<String> words, char separator) {
        List<String> res = new ArrayList<>();
        for (String word: words) {
            int index = 0;
            for (int i = 0; i < word.length(); i++) {
                if (word.charAt(i) == separator) {
                    // 截取
                    String substring = word.substring(index, i);
                    // 非空串
                    if (!"".equals(substring)) {
                        res.add(substring);
                    }
                    index = i + 1;
                }
            }
            if (index < word.length()) {
                res.add(word.substring(index));
            }
        }
        return res;
    }

    public static void main(String[] args) {
        List<String> words = Arrays.asList("one.two.three", "four.five", "six");
        char separator = '.';
        String a = "$easy$";
        String split = "\\$";
        List<String> strings = splitWord(a, split);
        String[] splits = a.split(split);
//        List<String> strings = splitWordsBySeparator(words, separator);
//        strings.forEach(System.out::println);
    }
}
